import request from './index'

// 用户相关API

/**
 * 获取用户信息
 * @returns {Promise}
 */
export const getUserInfo = () => {
  return request.get('/user/info')
}

/**
 * 更新用户信息
 * @param {Object} data - 用户数据
 * @param {string} data.nickname - 昵称
 * @param {string} data.avatar - 头像URL
 * @param {string} data.gender - 性别
 * @param {string} data.birthday - 生日
 * @param {string} data.phone - 手机号
 * @param {string} data.email - 邮箱
 * @param {string} data.address - 地址
 * @param {string} data.bio - 个人简介
 * @returns {Promise}
 */
export const updateUserInfo = (data) => {
  return request.put('/user/info', data)
}

/**
 * 上传头像
 * @param {File} file - 头像文件
 * @returns {Promise}
 */
export const uploadAvatar = (file) => {
  const formData = new FormData()
  formData.append('avatar', file)
  return request.post('/user/avatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 修改密码
 * @param {Object} data - 密码数据
 * @param {string} data.oldPassword - 旧密码
 * @param {string} data.newPassword - 新密码
 * @param {string} data.confirmPassword - 确认密码
 * @returns {Promise}
 */
export const changePassword = (data) => {
  return request.put('/user/password', data)
}

/**
 * 绑定手机号
 * @param {Object} data - 绑定数据
 * @param {string} data.phone - 手机号
 * @param {string} data.code - 验证码
 * @returns {Promise}
 */
export const bindPhone = (data) => {
  return request.post('/user/bind-phone', data)
}

/**
 * 解绑手机号
 * @param {Object} data - 解绑数据
 * @param {string} data.code - 验证码
 * @returns {Promise}
 */
export const unbindPhone = (data) => {
  return request.post('/user/unbind-phone', data)
}

/**
 * 绑定邮箱
 * @param {Object} data - 绑定数据
 * @param {string} data.email - 邮箱
 * @param {string} data.code - 验证码
 * @returns {Promise}
 */
export const bindEmail = (data) => {
  return request.post('/user/bind-email', data)
}

/**
 * 解绑邮箱
 * @param {Object} data - 解绑数据
 * @param {string} data.code - 验证码
 * @returns {Promise}
 */
export const unbindEmail = (data) => {
  return request.post('/user/unbind-email', data)
}

/**
 * 获取学习统计
 * @param {Object} params - 查询参数
 * @param {string} params.period - 统计周期 (day|week|month|year)
 * @param {string} params.startDate - 开始日期
 * @param {string} params.endDate - 结束日期
 * @returns {Promise}
 */
export const getLearningStats = (params) => {
  return request.get('/user/learning-stats', { params })
}

/**
 * 获取学习日历
 * @param {Object} params - 查询参数
 * @param {string} params.year - 年份
 * @param {string} params.month - 月份
 * @returns {Promise}
 */
export const getLearningCalendar = (params) => {
  return request.get('/user/learning-calendar', { params })
}

/**
 * 获取学习记录
 * @param {Object} params - 查询参数
 * @param {string} params.type - 记录类型 (course|exam|practice)
 * @param {number} params.page - 页码
 * @param {number} params.limit - 每页数量
 * @returns {Promise}
 */
export const getLearningRecords = (params) => {
  return request.get('/user/learning-records', { params })
}

/**
 * 获取成就列表
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export const getAchievements = (params) => {
  return request.get('/user/achievements', { params })
}

/**
 * 获取证书列表
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export const getCertificates = (params) => {
  return request.get('/user/certificates', { params })
}

/**
 * 下载证书
 * @param {string|number} certificateId - 证书ID
 * @returns {Promise}
 */
export const downloadCertificate = (certificateId) => {
  return request.get(`/user/certificates/${certificateId}/download`, {
    responseType: 'blob'
  })
}

/**
 * 获取积分记录
 * @param {Object} params - 查询参数
 * @param {string} params.type - 记录类型
 * @param {number} params.page - 页码
 * @param {number} params.limit - 每页数量
 * @returns {Promise}
 */
export const getPointsHistory = (params) => {
  return request.get('/user/points-history', { params })
}

/**
 * 签到
 * @returns {Promise}
 */
export const checkIn = () => {
  return request.post('/user/check-in')
}

/**
 * 获取签到记录
 * @param {Object} params - 查询参数
 * @param {string} params.year - 年份
 * @param {string} params.month - 月份
 * @returns {Promise}
 */
export const getCheckInRecords = (params) => {
  return request.get('/user/check-in-records', { params })
}

/**
 * 获取消息列表
 * @param {Object} params - 查询参数
 * @param {string} params.type - 消息类型
 * @param {boolean} params.unread - 是否只显示未读
 * @param {number} params.page - 页码
 * @param {number} params.limit - 每页数量
 * @returns {Promise}
 */
export const getMessages = (params) => {
  return request.get('/user/messages', { params })
}

/**
 * 标记消息为已读
 * @param {string|number} messageId - 消息ID
 * @returns {Promise}
 */
export const markMessageAsRead = (messageId) => {
  return request.put(`/user/messages/${messageId}/read`)
}

/**
 * 批量标记消息为已读
 * @param {Array} messageIds - 消息ID数组
 * @returns {Promise}
 */
export const markMessagesAsRead = (messageIds) => {
  return request.put('/user/messages/batch-read', { messageIds })
}

/**
 * 删除消息
 * @param {string|number} messageId - 消息ID
 * @returns {Promise}
 */
export const deleteMessage = (messageId) => {
  return request.delete(`/user/messages/${messageId}`)
}

/**
 * 获取未读消息数量
 * @returns {Promise}
 */
export const getUnreadMessageCount = () => {
  return request.get('/user/messages/unread-count')
}

/**
 * 获取通知设置
 * @returns {Promise}
 */
export const getNotificationSettings = () => {
  return request.get('/user/notification-settings')
}

/**
 * 更新通知设置
 * @param {Object} data - 设置数据
 * @returns {Promise}
 */
export const updateNotificationSettings = (data) => {
  return request.put('/user/notification-settings', data)
}

/**
 * 获取隐私设置
 * @returns {Promise}
 */
export const getPrivacySettings = () => {
  return request.get('/user/privacy-settings')
}

/**
 * 更新隐私设置
 * @param {Object} data - 设置数据
 * @returns {Promise}
 */
export const updatePrivacySettings = (data) => {
  return request.put('/user/privacy-settings', data)
}

/**
 * 获取学习偏好设置
 * @returns {Promise}
 */
export const getLearningPreferences = () => {
  return request.get('/user/learning-preferences')
}

/**
 * 更新学习偏好设置
 * @param {Object} data - 设置数据
 * @returns {Promise}
 */
export const updateLearningPreferences = (data) => {
  return request.put('/user/learning-preferences', data)
}

/**
 * 获取关注列表
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export const getFollowing = (params) => {
  return request.get('/user/following', { params })
}

/**
 * 获取粉丝列表
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export const getFollowers = (params) => {
  return request.get('/user/followers', { params })
}

/**
 * 关注用户
 * @param {string|number} userId - 用户ID
 * @returns {Promise}
 */
export const followUser = (userId) => {
  return request.post(`/user/follow/${userId}`)
}

/**
 * 取消关注用户
 * @param {string|number} userId - 用户ID
 * @returns {Promise}
 */
export const unfollowUser = (userId) => {
  return request.delete(`/user/follow/${userId}`)
}

/**
 * 获取用户动态
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export const getUserActivities = (params) => {
  return request.get('/user/activities', { params })
}

/**
 * 发布动态
 * @param {Object} data - 动态数据
 * @returns {Promise}
 */
export const publishActivity = (data) => {
  return request.post('/user/activities', data)
}

/**
 * 删除动态
 * @param {string|number} activityId - 动态ID
 * @returns {Promise}
 */
export const deleteActivity = (activityId) => {
  return request.delete(`/user/activities/${activityId}`)
}

/**
 * 获取收藏夹列表
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export const getFavorites = (params) => {
  return request.get('/user/favorites', { params })
}

/**
 * 创建收藏夹
 * @param {Object} data - 收藏夹数据
 * @returns {Promise}
 */
export const createFavoriteFolder = (data) => {
  return request.post('/user/favorite-folders', data)
}

/**
 * 更新收藏夹
 * @param {string|number} folderId - 收藏夹ID
 * @param {Object} data - 收藏夹数据
 * @returns {Promise}
 */
export const updateFavoriteFolder = (folderId, data) => {
  return request.put(`/user/favorite-folders/${folderId}`, data)
}

/**
 * 删除收藏夹
 * @param {string|number} folderId - 收藏夹ID
 * @returns {Promise}
 */
export const deleteFavoriteFolder = (folderId) => {
  return request.delete(`/user/favorite-folders/${folderId}`)
}

/**
 * 获取学习计划列表
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export const getLearningPlans = (params) => {
  return request.get('/user/learning-plans', { params })
}

/**
 * 创建学习计划
 * @param {Object} data - 计划数据
 * @returns {Promise}
 */
export const createLearningPlan = (data) => {
  return request.post('/user/learning-plans', data)
}

/**
 * 更新学习计划
 * @param {string|number} planId - 计划ID
 * @param {Object} data - 计划数据
 * @returns {Promise}
 */
export const updateLearningPlan = (planId, data) => {
  return request.put(`/user/learning-plans/${planId}`, data)
}

/**
 * 删除学习计划
 * @param {string|number} planId - 计划ID
 * @returns {Promise}
 */
export const deleteLearningPlan = (planId) => {
  return request.delete(`/user/learning-plans/${planId}`)
}

/**
 * 完成学习计划任务
 * @param {string|number} planId - 计划ID
 * @param {string|number} taskId - 任务ID
 * @returns {Promise}
 */
export const completeLearningTask = (planId, taskId) => {
  return request.post(`/user/learning-plans/${planId}/tasks/${taskId}/complete`)
}

/**
 * 获取学习目标
 * @returns {Promise}
 */
export const getLearningGoals = () => {
  return request.get('/user/learning-goals')
}

/**
 * 设置学习目标
 * @param {Object} data - 目标数据
 * @returns {Promise}
 */
export const setLearningGoals = (data) => {
  return request.post('/user/learning-goals', data)
}

/**
 * 更新学习目标
 * @param {string|number} goalId - 目标ID
 * @param {Object} data - 目标数据
 * @returns {Promise}
 */
export const updateLearningGoal = (goalId, data) => {
  return request.put(`/user/learning-goals/${goalId}`, data)
}

/**
 * 注销账号
 * @param {Object} data - 注销数据
 * @param {string} data.reason - 注销原因
 * @param {string} data.password - 密码确认
 * @returns {Promise}
 */
export const deleteAccount = (data) => {
  return request.post('/user/delete-account', data)
}

/**
 * 导出用户数据
 * @returns {Promise}
 */
export const exportUserData = () => {
  return request.get('/user/export-data', {
    responseType: 'blob'
  })
}

/**
 * 获取第三方账号绑定状态
 * @returns {Promise}
 */
export const getThirdPartyBindings = () => {
  return request.get('/user/third-party-bindings')
}

/**
 * 绑定第三方账号
 * @param {string} provider - 第三方平台
 * @param {Object} data - 绑定数据
 * @returns {Promise}
 */
export const bindThirdPartyAccount = (provider, data) => {
  return request.post(`/user/bind-${provider}`, data)
}

/**
 * 解绑第三方账号
 * @param {string} provider - 第三方平台
 * @returns {Promise}
 */
export const unbindThirdPartyAccount = (provider) => {
  return request.delete(`/user/bind-${provider}`)
}